//
//  TopTabView.m
//  TrainTree
//
//  Created by wanghui on 2017/9/5.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "TopTabView.h"
#import "TopTabCell.h"
static NSString *ID = @"TopTabCell";

@interface TopTabView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{

    int _first;
    PageType _type;
}
@end

@implementation TopTabView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(119, 40);
//    flowLayout.minimumInteritemSpacing = -1;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    [self.collectionView setCollectionViewLayout:flowLayout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TopTabCell" bundle:nil] forCellWithReuseIdentifier:ID];
//    self.dataArray = @[@"通用",@"运营",@"物流",@"人力",@"财务",@"精益",@"求精",@"HR"];
//    self.selectArray = [[NSMutableArray alloc] initWithArray: @[@"1",@"0",@"0",@"0",@"0",@"0",@"0",@"0"]];
}

- (NSMutableArray *)themeArray
{
    return _themeArray ?: (_themeArray = [NSMutableArray array]);
}
- (NSArray *)dataArray
{
    return _dataArray ?:(_dataArray = [NSArray array]);
}
- (NSMutableArray *)selectArray
{
    return _selectArray ?:(_selectArray = [NSMutableArray array]);
}
- (void)configWithData:(NSArray *)array
{

}
#pragma mark - collectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.themeArray.count;
//    return self.cellArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(ScreenWidth / self.themeArray.count, 40.0f);
}
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0,0,0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    TopTabCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
//    [cell configWithModel:self.cellArray[indexPath.row]];
    if (self.themeArray.count > 0) {
        cell.title.text = self.themeArray[indexPath.item][@"themeName"];
    }
    if (self.themeArray.count > 0) {
        if ([self.selectArray[indexPath.item] isEqualToString: @"1"]) {
            cell.line.hidden = NO;

            cell.title.textColor = rgb(74.0f, 74.0f, 74.0f);
        }else{
            cell.line.hidden = YES;
            cell.title.textColor = rgb(164.0f, 164.0f, 164.0f);
        }
    }

    UIColor *tmpColor = universeColor;

    _type = (PageType)indexPath.item % 6;
    if (_type == PageTypeUniverse) {

        tmpColor = universeColor;
    } else if (_type == PageTypeLeader) {

        tmpColor = leaderColor;
    } else if (_type == PageTypeOperation) {

        tmpColor = operationColor;
    } else if (_type == PageTypeHr) {

        tmpColor = hrColor;
    } else if (_type == PageTypeFinance) {
        NSLog(@"======finance");
        tmpColor = financeColor;
    } else if (_type == PageTypeLec) {
        tmpColor = lecColor;
        NSLog(@"======lec");
    }else{
        NSLog(@"======都没走");
    }

    cell.line.backgroundColor = tmpColor;

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.themeArray.count > 0) {
        for (int i = 0; i < self.selectArray.count; i++) {
            if (indexPath.item == i) {
                self.selectArray[i] = @"1";
            }else{
                self.selectArray[i] = @"0";
            }
        }
    }

//    [self.collectionView reloadData];

    if(_delegate && [_delegate respondsToSelector:@selector(topTabViewTouch:index:)]) {
    // 把类型ID传过去
        [_delegate topTabViewTouch: self.themeArray[indexPath.item][@"themeId"]  index:indexPath.item];
    }

}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    TopTabCell * cell = (TopTabCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.line.hidden = !cell.selected;

}


@end
